package com.servlet;

import com.entity.Students;
import com.tools.Utils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SelectAll {
    public List<Students> getList(){
        //用工具包构造Connection连接对象
        Connection conn=Utils.getConn();
        List<Students> list=new ArrayList<>();
        try {
            //用连接对象构造sql执行器
            Statement statement=conn.createStatement();
            //用执行器执行sql返回结果集
            ResultSet resultSet = statement.executeQuery("SELECT * FROM students;");
            //将结果集遍历封装至实体对象然后再封装至List集合
            while (resultSet.next()){
                Students stu=new Students();
                stu.setSid(resultSet.getInt("sid"));
                stu.setSname(resultSet.getString("sname"));
                stu.setSage(resultSet.getString("sage"));
                stu.setSclass(resultSet.getString("sclass"));
                list.add(stu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
